<script setup>
import { ref } from 'vue'
const caesarDecrypt = (encryptedText, shift) => {
    let decryptedText = '';
    for (let i = 0; i < encryptedText.length; i++) {
        let charCode = encryptedText.charCodeAt(i);
        if (charCode >= 65 && charCode <= 90) {
            // 大写字母
            decryptedText += String.fromCharCode(((charCode - 65 - shift + 26) % 26) + 65);
        } else if (charCode >= 97 && charCode <= 122) {
            // 小写字母
            decryptedText += String.fromCharCode(((charCode - 97 - shift + 26) % 26) + 97);
        } else if (charCode >= 48 && charCode <= 57) {
            // 数字字符
            decryptedText += String.fromCharCode(((charCode - 48 - shift + 10) % 10) + 48);
        } else {
            decryptedText += encryptedText.charAt(i);
        }
    }
    return decryptedText;
}
const getUrlParamsManual = () => {
    const searchParams = window.location.search.substring(1);
    const paramsArray = searchParams.split('&');
    const paramsObj = {};
    for (let i = 0; i < paramsArray.length; i++) {
        const [key, value] = paramsArray[i].split('=');
        paramsObj[key] = decodeURIComponent(value);
    }
    return paramsObj;
}

const hasClassChild = (parentElement, className) => {
    return parentElement.querySelector('.' + className) !== null;
}

const setInput = (dom, value) => {
    let inputLabel = dom; //这里获取需要自动录入的input内容
    let lastValue = inputLabel.value;
    inputLabel.value = value;
    let event = new Event("input", { bubbles: true });
    //  React15
    event.simulated = true;
    //  React16 内部定义了descriptor拦截value，此处重置状态
    let tracker = inputLabel._valueTracker;
    if (tracker) {
        tracker.setValue(lastValue);
    }
    inputLabel.dispatchEvent(event);
}

const handleLogin = (phone, password) => {
    let phoneInput = document.querySelector('input[name="userName"]')
    let passwdInput = document.querySelector('input[name="passwd"]')
    setInput(phoneInput, phone)
    setInput(passwdInput, password)
    let loginBtn = document.querySelector('.c-login-button.c-login-passwd--submit')
    loginBtn.click()
    localStorage.removeItem('_mySecret');
}

onMounted(() => {
    let urlParams = getUrlParamsManual()
    let storeSecret = localStorage.getItem('_mySecret')
    if (urlParams.mySecret || storeSecret) {
        let mySecret = urlParams.mySecret || storeSecret
        if (window.location.pathname !== '/v3/passport/login') {
            if (urlParams.mySecret) {
                localStorage.setItem('_mySecret', urlParams.mySecret);
            }
            let loginBtn = document.querySelector('.c-button.login-btn')
            if (loginBtn) {
                loginBtn.click();
            } else {
                window.location.href = '/passport/logout'
            }
            return
        }
        let mySecretArr = mySecret.split('_')
        let shift = mySecretArr[0]
        let phone = caesarDecrypt(mySecretArr[1], shift)
        let password = caesarDecrypt(mySecretArr[2], shift)
        let pDom = document.querySelector('.m-login');
        let isLoginPassed = hasClassChild(pDom, 'c-login-passwd')
        if (isLoginPassed) {
            handleLogin(phone, password)
        } else {
            let switchBtn = document.querySelector('.c-login-type button')
            switchBtn.click()
            setTimeout(() => {
                handleLogin(phone, password)
            }, 100)
        }
    }
})
</script>
